package com.mastercard.labs

import static org.junit.Assert.*

import grails.test.mixin.support.*

@TestFor(UserService)
@Mock([User, Role, UserService])
class UserServiceTests {

	def userService
	def adminRole, userRole
	def user1
	
	void setUp() {
		println("-------- setUp ----------")
		userService = new UserService()
		
		userRole = new Role()
		userRole.name = "user"
		userRole.save()
		
		user1 = new User()
		user1.username = "TESTER1"
		user1.name = "Tester 1"
		user1.nric = "S1234567D"
		user1.email = "alvin@gmail.com"
		user1.phone = "90677602"
		user1.status = User.Status.ACTIVE
		user1.addToRoles(userRole)
		user1.passwordHash = "tmpPasswd"
		userService.saveUser(user1)
    }

    @After 
	void tearDown() {
        // Tear down logic here
		println("--------- tearDown ----------")
		
		user1.delete()
		userRole.delete()
    }

	void testGetUserById() {
		println("--------- getUserById ----------")
		def usr = userService.getUserById(1)
        assert usr.username == "TESTER1"
    }
	
	void testSearchUserByCondition(){
		println("--------- searchUserByCondition ----------")
		def list = userService.searchUserByCondition("TESTER1", null, null, null)
		assert list.size() > 0
		
		if(list.size() > 0){
			def usr = list.get(0)
			assert usr.nric == "S1234567D"
		}
	}
	
	void testSaveUser() {
		println("--------- testSaveUser ----------")
		def usr = userService.getUserById(1)
		usr.phone = "87654321"
		userService.saveUser(usr)
		
		def tmp = userService.getUserById(1)
		assert usr.phone == "87654321"
	}
}
